{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68f0bf7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 猎聘数据实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57a76697",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 该项目为猎聘数据挖掘和分析项目\n",
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "import time\n",
    "\n",
    "def liepin_data(用户输入的地区，用户输入职位):\n",
    "    url = \"https://apic.liepin.com/api/com.liepin.searchfront4c.pc-search-job\"\n",
    "    payload = {\n",
    "        \"data\": {\n",
    "            \"mainSearchPcConditionForm\": {\n",
    "                \"city\": 地区编码字典[用户输入的地区],\n",
    "                \"dq\": 地区编码字典[用户输入的地区],\n",
    "                \"pubTime\": \"\",\n",
    "                \"currentPage\": 0,\n",
    "                \"pageSize\": 40,\n",
    "                \"key\": 用户输入职位,\n",
    "                \"suggestTag\": \"\",\n",
    "                \"workYearCode\": \"0\",\n",
    "                \"compId\": \"\",\n",
    "                \"compName\": \"\",\n",
    "                \"compTag\": \"\",\n",
    "                \"industry\": \"\",\n",
    "                \"salary\": \"\",\n",
    "                \"jobKind\": \"\",\n",
    "                \"compScale\": \"\",\n",
    "                \"compKind\": \"\",\n",
    "                \"compStage\": \"\",\n",
    "                \"eduLevel\": \"\"\n",
    "            },\n",
    "            \"passThroughForm\": {\n",
    "                \"scene\": \"input\",\n",
    "                \"skId\": \"\",\n",
    "                \"fkId\": \"\",\n",
    "                \"ckId\": \"h2c8pxojavrmo1w785z7ueih2ybfpux8\",\n",
    "                \"suggest\": None\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "\n",
    "    # set the headers\n",
    "    headers = {\n",
    "        'Accept': 'application/json, text/plain, */*',\n",
    "        'Accept-Encoding': 'gzip, deflate, br',\n",
    "        'Accept-Language': 'zh-CN,zh;q=0.9',\n",
    "        'Cache-Control': 'no-cache',\n",
    "        'Connection': 'keep-alive',\n",
    "        'Content-Length': '412',\n",
    "        'Content-Type': 'application/json;charset=UTF-8;',\n",
    "        'Cookie':'inited_user=daf7251f92024e8969feb28b0e9ad34c; __gc_id=0baa2ddaa7774d8fba2b9c2c3d8ba166; __uuid=1670205465393.76; XSRF-TOKEN=ENzGaqkHSwuu77YYn7m0IQ; _ga=GA1.1.1852247760.1685687743; __tlog=1685687751918.65%7C00000000%7C00000000%7C00000000%7C00000000; Hm_lvt_a2647413544f5a04f00da7eee0d5e200=1685687755; acw_tc=276077d516856877547452401ee7e4c0793a7cc8414b4954d6510b1d7f452a; UniqueKey=95507c72a8d5ae141a667e00ad0d9493; liepin_login_valid=0; lt_auth=uOZeOXQGxlzxtXfR3zQN4vociI39UWvIpX8EhE0Ahoe%2BCqG04PngSwOGq7EExAMhmhImcMULN7j7M%2BD2wXJD7UcWwGqnl4CyvOW92GECSuBcN8W2vezHl8zRQpQcl0AC8nFbtkIL%2BQ%3D%3D; access_system=C; user_roles=0; user_photo=5f8fa3a679c7cc70efbf444e08u.png; user_name=%E8%AE%B8%E6%99%BA%E8%B6%85; need_bind_tel=false; new_user=false; c_flag=fa43f4d55f3df63a96a7b4f194e214d4; inited_user=daf7251f92024e8969feb28b0e9ad34c; Hm_lpvt_a2647413544f5a04f00da7eee0d5e200=1685688821; imId=c5f9b89f8466dffe6882ca1e5431db9c; imId_0=c5f9b89f8466dffe6882ca1e5431db9c; imClientId=c5f9b89f8466dffeb1921abcfab3aed0; imClientId_0=c5f9b89f8466dffeb1921abcfab3aed0; imApp_0=1; __session_seq=7; __uv_seq=7; fe_im_socketSequence_new_0=1_1_1; fe_im_opened_pages=; fe_im_connectJson_0=%7B%220_95507c72a8d5ae141a667e00ad0d9493%22%3A%7B%22socketConnect%22%3A%222%22%2C%22connectDomain%22%3A%22liepin.com%22%7D%7D; _ga_54YTJKWN86=GS1.1.1685687743.1.1.1685688839.0.0.0',\n",
    "        'Host': 'apic.liepin.com',\n",
    "        'Origin': 'https://www.liepin.com',\n",
    "        'Pragma': 'no-cache',\n",
    "        'Referer': 'https://www.liepin.com/',\n",
    "        'sec-ch-ua': '\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"',\n",
    "        'sec-ch-ua-mobile': '?0',\n",
    "        'sec-ch-ua-platform': '\"Windows\"',\n",
    "        'Sec-Fetch-Dest': 'empty',\n",
    "        'Sec-Fetch-Mode': 'cors',\n",
    "        'Sec-Fetch-Site': 'same-site',\n",
    "        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',\n",
    "        'X-Client-Type': 'web',\n",
    "        'X-Fscp-Bi-Stat': '{\"location\": \"https://www.liepin.com/zhaopin/?inputFrom=www_index&workYearCode=0&key=%E4%BA%A7%E5%93%81%E7%BB%8F%E7%90%86&scene=input&ckId=htihov8m2frxgy6ywo2wsg2gncnydzlb&dq=\"}',\n",
    "        'X-Fscp-Fe-Version': '',\n",
    "        'X-Fscp-Std-Info': '{\"client_id\": \"40108\"}',\n",
    "        'X-Fscp-Trace-Id': 'e21cd0d2-44d4-4aba-b8bf-9c44f5c5f453',\n",
    "        'X-Fscp-Version': '1.1',\n",
    "        'X-Requested-With': 'XMLHttpRequest',\n",
    "        'X-XSRF-TOKEN': 'ENzGaqkHSwuu77YYn7m0IQ'\n",
    "    }\n",
    "    # 1.首次获取页面信息\n",
    "    r = requests.post(url, data=json.dumps(payload), headers=headers)\n",
    "    response_data = r.json()\n",
    "    page = response_data['data']['pagination']['totalPage']\n",
    "    \n",
    "    # 2.翻页获取数据\n",
    "    response_df = []\n",
    "    for i in range(page): # 需要判断页面的数据有多少页\n",
    "        payload['data']['mainSearchPcConditionForm']['currentPage']=i\n",
    "        r = requests.post(url, data=json.dumps(payload), headers=headers)\n",
    "        response_data = r.json()\n",
    "        df = pd.json_normalize(response_data['data']['data']['jobCardList'])\n",
    "        response_df.append(df)\n",
    "        \n",
    "    # 3.整理表格输出数据\n",
    "    df = pd.concat(response_df)\n",
    "    key = payload['data']['mainSearchPcConditionForm']['key']\n",
    "    output_time = str(time.localtime().tm_mon)\\\n",
    "             +str(time.localtime().tm_mday)+'_'\\\n",
    "             +str(time.localtime().tm_hour) \\\n",
    "             +str(time.localtime().tm_min)\n",
    "    df.to_excel( key +'_liepin_'+output_time+'.xlsx')\n",
    "    \n",
    "    # 4.返回值\n",
    "    return\"当前数据已导出，数据量为：\",len(df),\"行\"\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a0d50a",
   "metadata": {},
   "outputs": [],
   "source": [
    "用户输入职位 = input(\"\")\n",
    "用户输入的地区"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d170b08",
   "metadata": {},
   "outputs": [],
   "source": [
    "地区编码字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e11d14fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "page = response"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
